<template>
  <div>
    <div
      style="
        margin-bottom: 20px;
        margin-left: 10px;
        margin-right: 20px;
        float: left;
      "
    >
      <el-button round @click="openDialog">规划小区</el-button>
    </div>
    <!-- 搜索框 -->
    <el-form
      ref="administratorForm"
      :inline="true"
      class="demo-form-inline"
      :model="administrator"
      :rules="administratorrules"
    >
      <el-form-item prop="name">
        <el-input
          placeholder="输入管理员姓名查询"
          v-model="administrator.name"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="onSubmit"
          :disabled="!administrator.name ? true : false"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="areaList"
      style="width: 100%"
      class="area_table"
      ref="areaTable"
      highlight-current-row
      @row-click="handleCurrentChange"
      border
      height="313"
    >
      <el-table-column type="index" label="序号" width="150" align="center">
      </el-table-column>
      <el-table-column
        label="创建时间"
        width="200"
        align="center"
        property="create_time"
      ></el-table-column>
      <el-table-column
        label="管理人"
        width="100"
        align="center"
        property="name"
      ></el-table-column>
      <el-table-column
        label="电话"
        width="200"
        align="center"
        property="phone"
      ></el-table-column>
      <el-table-column
        label="管辖区域"
        width="200"
        align="center"
        property="administer"
      ></el-table-column>
      <el-table-column
        label="授权人"
        width="200"
        align="center"
        property="auth_name"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="openDialog(scope.row)">编辑</el-button>
          &nbsp;&nbsp;&nbsp;
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentPage"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 新增窗口 -->
    <el-dialog title="规划小区" :visible.sync="dialogFormVisible" width="400px">
      <el-form
        :model="area"
        status-icon
        ref="areaForm"
        label-width="100px"
        label-position="left"
        style="width: 90%"
        :rules="areaRules"
      >
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="area.name"
            placehilder="请输入管理人姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="area.phone" placehilder="请输入电话"></el-input>
        </el-form-item>
        <el-form-item label="管辖区">
          <el-select v-model="area.administer" placeholder="输入管辖区">
            <el-option label="A" value="A"></el-option>
            <el-option label="B" value="B"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="!_id ? addData('areaForm') : handleUpdate('areaForm')"
          >确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import areaApi from "@/api/area";
import memoryUtils from "@/utils/memoryUtils";
import { formateDate } from "@/utils/dateUtils";
var trim = (rule, value, callback) => {
  // console.log(!!value);
  // value = value.trim()
  if (!value) {
    callback(new Error("输入值不能为空"));
  }
};
var validatePhone = (rule, value, callback) => {
  // console.log(value);
  value = value.trim();
  // 提高性能，判断value是否有值有值返回长度
  const length = value && value.length;
  const pwdReg =
    /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
  if (value === "") {
    callback(new Error("请输入账号"));
  } else if (!pwdReg.test(value)) {
    callback(new Error("请输入正确的手机号码"));
  } else {
    callback();
  }
};
export default {
  data() {
    return {
      administrator: {
        name: "",
      },
      _id: null,
      auth_name: memoryUtils.user.username,
      total: 0,
      size: 5, //当前页个数
      currentPage: 1, //当前页
      dialogFormVisible: false,
      auth_time: Number,
      areaChange: null, //存储更改信息
      area: {
        name: "",
        phone: "",
        administer: "",
      },
      administratorrules: {
        name: [
          {
            validator: trim,
            message: "输入值不能为空",
            trigger: "blur",
          },
        ],
      },
      areaRules: {
        name: [{ required: true, message: "名称必须输入", trigger: "blur" }],
        phone: [
          {
            required: true,
            validator: validatePhone,
            trigger: ["change", "tigger"],
          },
        ],
      },
      currentRow: null,
      areaList: [], //存储列表
    };
  },

  components: {},
  mounted() {
    this.condition();
    // this.on()
  },
  methods: {
    // on() {
    //   setInterval(() => {
    //     console.log(this.administrator.name);
    //   }, 5000);
    // },
    // 查询
    onSubmit() {
      // console.log(this.administrator.name);
      let name = this.administrator.name.trim();
      areaApi.inquire(name).then((result) => {
        const res = result.data;
        if (res.status == 0) {
          this.areaList = res.data;
        } else {
          this.$message({
            message: res.msg,
            type: "warning",
          });
          this.condition();
        }
      });
      this.$nextTick(() => {
        this.$refs["administratorForm"].resetFields();
      });
    },
    // 获取分页信息
    condition() {
      areaApi.areaList(this.currentPage, this.size).then((response) => {
        const res = response.data;
        // console.log(res);
        if (res.status == 0) {
          this.total = res.data.total;
          res.data.data.map((item) => {
            item.create_time = formateDate(item.create_time);
          });
          this.areaList = res.data.data;
          if (this.areaList == 0) {
            this.currentPage = this.currentPage - 1;
            this.condition();
          }
        } else {
          this.$message({
            message: res.msg,
            type: "warning",
          });
        }
      });
    },
    // 改变当前页位置
    handleCurrentPage(currentPage) {
      this.currentPage = currentPage;
      // console.log(currentPage);
      this.condition();
    },
    // 每页条数
    handleSizeChange(size) {
      // console.log(size);
      this.size = size;
      this.condition();
    },
    // 自带函数获取当前点击行，获取两个参数，一个当前点击的一个为上一次点击的行
    handleCurrentChange(val) {
      // console.log(val);
      this.areaChange = val;
      //   console.log(this.areaChange._id);
    },
    // 删除
    handleDelete(index, row) {
      // console.log(index, row);
      areaApi.areaDelete(row).then((response) => {
        const res = response.data;
        // console.log(res);
        if (res.status == 0) {
          this.$message({
            message: res.data,
            type: "success",
          });
          this.condition();
        } else {
          this.$message({
            message: res.msg,
            type: "warning",
          });
        }
      });
    },
    //打开弹窗
    openDialog(row) {
      // console.log(row._id);
      // console.log(this._id);
      this._id = row._id;
      this.dialogFormVisible = true;
      if (this._id) {
        this.area.name = row.name;
        this.area.phone = row.phone;
        this.area.administer = row.administer;
        row = {};
      } else {
        // 异步事件
        this.area.name = "";
        this.area.phone = "";
        this.area.administer = "";
        this.$nextTick(() => {
          this.$refs["areaForm"].resetFields();
        });
      }
    },
    // 创建角色
    addData(areaForm) {
      console.log(this.area.administer);
      this.$refs["areaForm"].validate((valid) => {
        if (valid) {
          areaApi
            .areaadd(
              this.area.name,
              this.area.phone,
              this.auth_name,
              this.area.administer
            )
            .then((result) => {
              const res = result.data;
              if (res.status == 0) {
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
                this.condition();
              } else {
                this.$message({
                  message: res.msg,
                  type: "warning",
                });
              }
            });
          this.condition();
        }
        this.dialogFormVisible = false;
      });
    },
    // 更新角色
    handleUpdate(areaForm) {
      this.areaChange.name = this.area.name;
      this.areaChange.auth_name = this.auth_name;
      this.areaChange.phone = this.area.phone;
      this.areaChange.administer = this.area.administer;
      this.$refs["areaForm"].validate((valid) => {
        if (valid) {
          // console.log(this.areaChange);
          areaApi.updatearea(this.areaChange).then((area) => {
            const res = area.data;
            // console.log(res);
            if (res.status == 0) {
              this.$message({
                message: "更新成功",
                type: "success",
              });
              this._id = null;
              this.dialogFormVisible = false;
              this.condition();
            } else {
              this.$message({
                message: res.msg,
                type: "warning",
              });
            }
          });
        }
      });
    },
  },
};
</script>

<style scoped></style>